package com.xforceplus.bsstool.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.xforceplus.bsstool.config.ConfigMain;
import com.xforceplus.bsstool.db.NewBssDB;
import com.xforceplus.bsstool.db.NewUcenterDB;
import com.xforceplus.bsstool.db.OldBssDB;
import com.xforceplus.bsstool.db.OldUcenterDB;
import com.xforceplus.bsstool.log.LogMain;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServiceStep4 {

  /**
   * 获取老运营租户列表
   * @return
   */
  public static List<Map> getOldTenantList(List<Long> tenantIdList,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_group t where t.group_id  ")
            .append(ServiceCommon.getTenantIdInSql(tenantIdList));
    logger.info("执行老运营租户获取，执行SQL: "+sql.toString());
    return OldBssDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营租户列表
   * @return
   */
  public static Map<String,Map> getNewTenantMap(List<Long> tenantIdList,LogMain logger) throws Exception {
    Map<String,Map> newTenantMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_tenant t where t.tenant_id  ")
            .append(ServiceCommon.getTenantIdInSql(tenantIdList));
    logger.info("执行新运营租户获取，执行SQL: "+sql.toString());
    List<Map> newTenantList = NewUcenterDB.getSqlResult(sql.toString());
    for (Map newTenantMap : newTenantList){
      newTenantMapMap.put(String.valueOf(newTenantMap.get("tenant_id")),newTenantMap);
    }
    return newTenantMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareTenantWithNew(List<Map> oldTenantList,List<Long> tenantIdList,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newTenantMapMap  = getNewTenantMap(tenantIdList,logger);
    for (Map oldTenantMap : oldTenantList){
      System.out.println("开始执行租户比对 租户ID:"+oldTenantMap.get("group_id"));
      logger.info("开始执行租户比对 租户ID:"+oldTenantMap.get("group_id"));
      //查询新运营对应功能集
      Map newTenantMap =newTenantMapMap.get(String.valueOf(oldTenantMap.get("group_id")));
      if (newTenantMap == null){
        logger.warn("租户ID:"+oldTenantMap.get("group_id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同租户ID,继续执行数据信息比对 新运营租户："+ JSON.toJSONString(newTenantMap));
        //新老数据比对： tenant_name tenant_code status
        oldTenantMap.put("tenant_name",oldTenantMap.get("group_name"));
        oldTenantMap.put("tenant_code",oldTenantMap.get("group_code"));
        result = ServiceCommon.compareMapValue(oldTenantMap,newTenantMap,
                Lists.newArrayList("tenant_code","tenant_name"),
                "租户ID:"+oldTenantMap.get("group_id"),
                logger);
      }

      //重复租户检查
      List<Map> sameTenantList = NewUcenterDB.getSqlResult("select * from bss_tenant where tenant_id != "+oldTenantMap.get("group_id")
              +" and tenant_code = '"+oldTenantMap.get("group_code")+"'");
      if (!CollectionUtils.isEmpty(sameTenantList)){
        logger.error("老运营租户ID:"+oldTenantMap.get("group_id")+" 新运营存在重复租户(tenant_code重复)记录个数："+sameTenantList.size());
        result = false;
      }
      sameTenantList = NewUcenterDB.getSqlResult("select * from bss_tenant where tenant_id != "+oldTenantMap.get("group_id")
              +" and tenant_name = '"+oldTenantMap.get("group_name")+"'");
      if (!CollectionUtils.isEmpty(sameTenantList)){
        logger.error("老运营租户ID:"+oldTenantMap.get("group_id")+" 新运营存在重复租户(tenant_name重复)记录个数："+sameTenantList.size());
        result = false;
      }
    }
    return result;
  }



  /**
   * 获取老运营租户协同关系列表
   * @return
   */
  public static List<Map> getOldTenantCoopList(Object purchaserTenantId,LogMain logger) throws Exception {
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_company_coordination_rule t where t.seller_group_id not in(3400,3401)  and t.coordination_type = 'G' and t.purchaser_group_id = ")
            .append(purchaserTenantId).append(" limit ").append(ConfigMain.MAX_COOP_SELLER_CHECK_NUM);
    logger.info("执行老运营租户协同关系获取，执行SQL: "+sql.toString());
    return OldBssDB.getSqlResult(sql.toString());
  }
  /**
   * 获取新运营租户协同关系列表
   * @return
   */
  public static Map<String,Map> getNewTenantCoopMap(Object purchaserTenantId,LogMain logger) throws Exception {
    Map<String,Map> newTenantCoopMapMap  = new HashMap<>();
    StringBuilder sql = new StringBuilder();
    sql.append("select * from bss_company_coordination_rule t where t.seller_tenant_id not in(3400,3401)  and t.coordination_type = 'G' and t.purchaser_tenant_id = ")
            .append(purchaserTenantId).append(" limit ").append(ConfigMain.MAX_COOP_SELLER_CHECK_NUM);
    logger.info("执行新运营租户协同关系获取，执行SQL: "+sql.toString());
    List<Map> newTenantCoopList = NewBssDB.getSqlResult(sql.toString());
    for (Map newTenantCoopMap : newTenantCoopList){
      newTenantCoopMapMap.put(String.valueOf(newTenantCoopMap.get("coordination_rule_id")),newTenantCoopMap);
    }
    return newTenantCoopMapMap;
  }

  /**
   * 比对新运营功能集
   * @return
   */
  public static boolean compareTenantCoopWithNew(List<Map> oldTenantCoopList,Object purchaserTenantId,LogMain logger) throws Exception {
    boolean result = true;
    Map<String,Map> newTenantCoopMapMap  = getNewTenantCoopMap(purchaserTenantId,logger);
    for (Map oldTenantCoopMap : oldTenantCoopList){
      System.out.println("开始执行租户协同关系比对 租户协同关系ID:"+oldTenantCoopMap.get("coordination_rule_id"));
      logger.info("开始执行租户协同关系比对 租户协同关系ID:"+oldTenantCoopMap.get("coordination_rule_id"));
      //查询新运营对应功能集
      Map newTenantCoopMap =newTenantCoopMapMap.get(String.valueOf(oldTenantCoopMap.get("coordination_rule_id")));
      if (newTenantCoopMap == null){
        logger.warn("租户协同关系ID:"+oldTenantCoopMap.get("coordination_rule_id")+" 等待迁移中...");
      }else {
        logger.info("新老运营存在相同租户协同关系ID,继续执行数据信息比对 新运营租户协同关系："+ JSON.toJSONString(newTenantCoopMap));
        //新老数据比对： seller_tenant_id purchaser_tenant_id  purchaser_tenant_code, seller_tenant_code ,status
        oldTenantCoopMap.put("seller_tenant_id",oldTenantCoopMap.get("seller_group_id"));
        oldTenantCoopMap.put("business_type",oldTenantCoopMap.get("business_type"));
        oldTenantCoopMap.put("purchaser_tenant_id",oldTenantCoopMap.get("purchaser_group_id"));
        oldTenantCoopMap.put("purchaser_tenant_code",oldTenantCoopMap.get("purchaser_group_code"));
        oldTenantCoopMap.put("seller_tenant_code",oldTenantCoopMap.get("seller_group_code"));
        result = ServiceCommon.compareMapValue(oldTenantCoopMap,newTenantCoopMap,
                Lists.newArrayList("seller_tenant_id","purchaser_tenant_id","purchaser_tenant_code","seller_tenant_code","status"),
                "租户协同关系ID:"+oldTenantCoopMap.get("coordination_rule_id"),
                logger);
      }

      //重复租户协同关系检查
      List<Map> sameTenantCoopList = NewBssDB.getSqlResult("select * from bss_company_coordination_rule where coordination_type = 'G' and coordination_rule_id != "+oldTenantCoopMap.get("coordination_rule_id")
              +" and business_type = "+oldTenantCoopMap.get("business_type")
              +" and seller_tenant_id = "+oldTenantCoopMap.get("seller_group_id")
              +" and purchaser_tenant_id = "+oldTenantCoopMap.get("purchaser_group_id"));
      if (!CollectionUtils.isEmpty(sameTenantCoopList)){
        logger.error("老运营租户协同关系ID:"+oldTenantCoopMap.get("coordination_rule_id")+" 新运营存在重复租户协同关系记录个数："+sameTenantCoopList.size());
        result = false;
      }

    }
    return result;
  }






}
